package com.example.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Random;

@Aspect
@Component
public class PermissionAspect {
    private Random random = new Random();

    // 权限校验：add、delete方法执行前
    @Before("execution(* com.example.service.OrderService.add(..)) || execution(* com.example.service.OrderService.delete(..))")
    public void checkPermission(JoinPoint joinPoint) {
        int permission = random.nextInt(2); // 生成0或1的随机数
        if (permission == 0) {
            System.out.println("权限校验通过，允许执行 " + joinPoint.getSignature().getName() + " 方法");
        } else {
            System.out.println("权限校验不通过，终止 " + joinPoint.getSignature().getName() + " 方法执行");
            throw new RuntimeException("权限不足");
        }
    }
}